<template>
  <cu-custom bgColor="bg-yellow" :isBack="true">
    <block slot="backText">返回</block>
    <block slot="content">{{ type == 'create' ? '创建账本' : '编辑账本' }}</block>
  </cu-custom>
  <scroll-view scroll-y class="indexes" :style="[{height:'calc(78vh)'}]"
               :scroll-with-animation="true" :enable-back-to-top="true">
    <form>
      <view class="cu-form-group margin-top">
        <view class="title">账本名称</view>
        <input class="border" v-model="exchangeBookData.bookName" placeholder="请输入账本名称"/>
      </view>
      <view class="cu-form-group">
        <view class="title">账本描述</view>
        <textarea class="border" maxlength="-1" v-model="exchangeBookData.bookDesc" @input="textareaBInput"
                  placeholder="请输入账本描述信息"></textarea>
      </view>
      <view class="cu-form-group margin-top">
        <view class="title">默认账本</view>
        <switch @change="swatchDefaultBookFLag" :class="exchangeBookData.defaultFlag == 1?'checked':''"
                :checked="exchangeBookData.defaultFlag == 1?true:false"></switch>
      </view>
      <view class="cu-form-group margin-top">
        <view class="title">私密账本</view>
        <switch @change="swatchPrivateBookFLag" :class="exchangeBookData.privateFlag == 1?'checked':''"
                :checked="exchangeBookData.privateFlag == 1?true:false"></switch>
      </view>
    </form>
  </scroll-view>
  <view class="padding flex flex-direction padding-bottom fixed bottom">
    <button class="cu-btn bg-yellow text-white margin-tb-sm" @tap="exchangeBookSave">保存账本</button>
  </view>
</template>

<script setup lang="ts">
import {ExchangeBook} from "../../common/model/exchange";
import {ref} from "vue";
import {onLoad, onReady, onShow} from '@dcloudio/uni-app';
import {createExchangeBook, updateExchangeBook} from "../../common/api/exchange";

const type = ref('create')
const bookId = ref();

const exchangeBookData = ref({} as ExchangeBook)

const swatchDefaultBookFLag = (e) => {
  exchangeBookData.value.defaultFlag = e.detail.value ? 1 : 0
}
const swatchPrivateBookFLag = (e) => {
  exchangeBookData.value.privateFlag = e.detail.value ? 1 : 0
}

const exchangeBookSave = () => {
  if (type.value == 'create') {
    createExchangeBook(exchangeBookData.value).then(res => {
      console.log(res)
      uni.showToast({
        title: '创建账本成功！',
        icon: 'none',
        duration: 3000,
        success: function (res) {
          uni.navigateTo({
            url: '/pages/user/ExchangeBooksList',
            success(result) {
            }
          });
        }
      })
    }).catch(e => {
      uni.showToast({
        title: '创建账本失败！',
        icon: 'none',
        duration: 3000,
      })
    })
  } else {
    updateExchangeBook(exchangeBookData.value).then(res => {
      console.log(res)
      uni.showToast({
        title: '修改账本成功！',
        icon: 'none',
        duration: 3000,
        success: function (res) {
          uni.navigateTo({
            url: '/pages/user/ExchangeBooksList',
            success(result) {
            }
          });
        }
      })
    }).catch(e => {
      uni.showToast({
        title: '修改账本失败！',
        icon: 'none',
        duration: 3000,
      })
    })
  }
}

onLoad((options) => {
  console.log('接收传值：', options)
  exchangeBookData.value = JSON.parse(decodeURIComponent(options.book));
  type.value = options.type;
});

</script>

<style scoped>

</style>